<?php

//$ret = httpGet("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx12befecc9df03b8a&secret=541f9dae26fb5b2c58d2753cdbf88765");
//var_dump($ret);
//$ret = json_decode($ret, true);
//$access_token = $ret['access_token'];
$access_token = "44_gQ2XbbjjGhW8oeGCs1P-PEllEfx-NF-YZuAkj9VdpSdfoNYg2VBuQRpV7-KGVK-QGuLAkhwJixZB1sz-buuVNYHuSwIFGndYdEMZzMml-V62x5FJ5qTLACn6YKQ95yf0ATLL5mFT1CdAi0q8BOTgADAGTY";
//$val = httpPost("https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=".$access_token, json_encode(["path"=>"pages/fishground/detail?id=20"]));
$val = httpPost("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=".$access_token, json_encode(["scene"=>'?id=189',"page"=>"pages/fishground/detail","width"=>430]));
//$val = send_post("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=".$access_token, json_encode(["scene"=>'id=20',"page"=>"pages/fishground/detail","width"=>430]));
var_dump($val);
$ret = json_decode($val, true);
if($ret !== false) file_put_contents ("code.jpg", $val);
/**
 * GET请求
 */
function httpGet($url)
{
	$ch = curl_init();
	$header = array(
		'Content-Type: application/json');
	curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	$tmpInfo = curl_exec($ch);
	if (curl_errno($ch)) {
		return curl_error($ch);
	}
	curl_close($ch);
	return $tmpInfo;
}

/**
 * POST请求
 * */
function httpPost($url, $data = null)
{
	//var_dump($data);
	$ch = curl_init();
	$header = array(
		"Content-Type: application/json",
		'Content-Length: ' . strlen($data)
	);
	var_dump($header);
	var_dump($url);
	
	//curl_setopt($ch, CURLOPT_AUTOREFERER, true);
	
	//curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
	//curl_setopt($ch, CURLOPT_TIMEOUT, 5);
	//curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
	curl_setopt($ch, CURLOPT_URL, $url);
	//curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
	//curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
	curl_setopt($ch, CURLOPT_POST, true);
	curl_setopt($ch, CURLOPT_POSTFIELDS, $data);	
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($ch, CURLOPT_HEADER, 0);
	curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
	$tmpInfo = curl_exec($ch);
	if ($tmpInfo === false) {
        $error    = curl_error($ch);
        var_dump($error);
    }
	curl_close($ch);
	return $tmpInfo;
}

/**
 * 消息推送http
 * @param $url
 * @param $post_data
 * @return bool|string
 */
function send_post( $url, $post_data ) {
	$options = array(
		'http' => array(
			'method'  => 'POST',
			'header'  => 'Content-type:application/json',
			//header 需要设置为 JSON
			'content' => $post_data,
			'timeout' => 60
			//超时时间
		)
	);
	$context = stream_context_create( $options );
	$result = file_get_contents( $url, false, $context );
	return $result;
}